package nickthissen.iracing.parsing;

import com.google.gson.Gson;

import java.util.ArrayList;

import nickthissen.iracing.datamodels.Driver;
import nickthissen.iracing.datamodels.Result;

/**
 * Created by nthissen on 04/06/13.
 */
public class DriverListParser extends Parser
{
    @Override
    protected Result doParse(String source)
    {
        Result result = new Result();
        ArrayList<Driver> drivers = new ArrayList<Driver>();

        log("Start parsing json");
        Gson g = new Gson();
        SearchResults search = g.fromJson(source, SearchResults.class);
        log("Finished parsing json");

        if (search == null)
            log("SearchResult is null.");
        else
            log("Drivers found: " + search.searchRacers.size());

        for (SearchDriver d : search.searchRacers)
        {
            Driver driver = new Driver();
            driver.Username = HttpUtil.urlDecode(d.name);
            driver.Custid = d.custid;
            drivers.add(driver);
        }

        result.Object = drivers;
        result.Success = true;
        return result;
    }

    private class SearchResults
    {
        public ArrayList<SearchDriver> searchRacers;
    }

    private class SearchDriver
    {
        public String name;
        public int custid;
    }

    @Override
    protected String getLogName()
    {
        return "DriverListParser";
    }

    @Override
    protected Result createDefaultResultObject()
    {
        Result result = new Result();
        result.Message = "Error parsing member list.";
        return result;
    }
}
